#include <stdlib.h>
#include <stdio.h>
#include <cuda.h>

#include "tmr_cuda_num_int_elTRY.cu"

#define SCALAR float

#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
   if (code != cudaSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}


extern "C" int invokeKernel(
        int gridSize, 
        int blockSize,
        int* execution_parameters,
        SCALAR* gauss_dat, // integration points data of elements having given p
        SCALAR* shpfun_ref, // shape functions on a reference element
        SCALAR* el_data_in, // data for integration of NR_ELEMS_THIS_KERCALL elements
        SCALAR* stiff_mat_out // result of integration of NR_ELEMS_THIS_KERCALL elements
)
{
    dim3 blocks(gridSize, 1, 1);
    dim3 threadsPerBlock(blockSize, 1, 1);
    
    tmr_ocl_num_int_elTRY<<<240,64>>>(
                                execution_parameters,
                                gauss_dat,
                                shpfun_ref,
                                el_data_in,
                                stiff_mat_out);
    
    
    
    
 /*   
    int *dIn,*dOut;
    int *hIn,*hOut;
    
    int n = 10;
    int sizeBytes = n * sizeof(int);  
    
    hIn = (int*) malloc( sizeBytes );
    hOut = (int*) malloc( sizeBytes );
    
    cudaMalloc(&dIn, sizeBytes);
    cudaMalloc(&dOut, sizeBytes);
    
    cudaMemcpy(dIn, hIn, sizeBytes, cudaMemcpyHostToDevice);
    
    
    tempkernel<<<1,n>>>(dIn,dOut);
    
    cudaMemcpy(hOut, dOut, sizeBytes, cudaMemcpyDeviceToHost);
 */   
    
    
    gpuErrchk( cudaPeekAtLastError() );
    if(cudaDeviceSynchronize() == ::cudaSuccess)
        printf("\nKERNEL SUCCEEDED, values:\n");
   
    
    /*
    int i;
    for(i = 0 ; i < n ; ++i)
        printf("%d ",hOut[i]);
    */
    
    return 1;
}